#include <interface\input.h>
#include <iostream>
input::input(): inserMode( true ),
                roll( ROLL_NONE ),
                mouseX( 0 ),
                mouseY( 0 )
{
        for(int i = 0; i < 256; i++ )   keys[i] = false;
        for(int i = 0; i < 3; i++ )     click[i] = false;
}

input::~input(){}

bool    input::getKey(unsigned char kn){
    return keys[kn];
}
bool    input::bindKey(unsigned char kn){
    if( keys[kn] ){
        keys[kn] = false;
        return true;
    }
    return false;
}
void    input::setKey(unsigned char kn,bool st){
    keys[kn] = st;
}

bool    input::getClk(unsigned char cn){
    return click[cn];
}
bool    input::bindClk(unsigned char cn){
    if( click[cn] ){
        click[cn] = false;
        return true;
    }
    return false;
}
void    input::setClk(unsigned char cn,bool st){
    click[cn] = st;
}

char    input::getRoll(){
    return roll;
}
char    input::bindRoll(){
    if( roll == ROLL_NONE ) return ROLL_NONE;
    else if (roll == ROLL_UP ){
        roll = ROLL_NONE;
        return ROLL_UP;
    }
    roll = ROLL_NONE;
    return ROLL_DOWN;
}
void    input::setRoll(char st){
    roll = st;
}

int     input::getMouseX(){
    return mouseX;
}
int     input::getMouseY(){
    return mouseY;
}
void    input::setMousePos(int x,int y){
    mouseX = x;
    mouseY = y;
}

void input::setWinSize(int* sX,int* sY){
    winSizeX = sX;
    winSizeY = sY;
}

char    input::getLastTyped(){
    return lastchar;
}

char    input::bindLastTyped(){
    char stack = lastchar;
    lastchar = '\0';
    return stack;
}

void    input::setLastTyped(char ct){
    lastchar = ct;
}

bool    input::getInserMode(){
    return inserMode;
}
void    input::setInserMode(bool in){
    inserMode = in;
}

bool input::translateInputMsg(UINT msg,WPARAM wparam,LPARAM lparam){
    switch( msg ){
        case WM_KEYDOWN:						// Is A Key Being Held Down?
        {
            keys[wparam] = true;				// If So, Mark It As TRUE
            if( wparam == VK_INSERT ){
                inserMode = !inserMode;
            }
            return 0;						// Jump Back
        }
        case WM_KEYUP:							// Has A Key Been Released?
        {
            keys[wparam] = false;					// If So, Mark It As FALSE
            return 0;						// Jump Back
        }
        case WM_MOUSEMOVE:
        {
            mouseX = LOWORD(lparam);
            mouseY = *winSizeY - HIWORD(lparam)- 1;
            return 0;
        }
        case WM_LBUTTONDOWN:
        {
            mouseX = LOWORD(lparam);
            mouseY = *winSizeY - HIWORD(lparam)- 1;
            click[LEFT_CLICK] = true;
            return 0;
        }
        case WM_LBUTTONUP:
        {
            mouseX = LOWORD(lparam);
            mouseY = *winSizeY - HIWORD(lparam)- 1;
            click[LEFT_CLICK] = false;
            return 0;
        }
        case WM_RBUTTONDOWN:
        {
            mouseX = LOWORD(lparam);
            mouseY = *winSizeY - HIWORD(lparam)- 1;
            click[RIGHT_CLICK] = true;
            return 0;
        }
        case WM_RBUTTONUP:
        {
            mouseX = LOWORD(lparam);
            mouseY = *winSizeY - HIWORD(lparam)- 1;
            click[RIGHT_CLICK] = false;
            return 0;
        }
        case WM_MBUTTONDOWN:
        {
            mouseX = LOWORD(lparam);
            mouseY = *winSizeY - HIWORD(lparam)- 1;
            click[MIDDLE_CLICK] = true;
            return 0;
        }
        case WM_MBUTTONUP:
        {
            mouseX = LOWORD(lparam);
            mouseY = *winSizeY - HIWORD(lparam) - 1;
            click[MIDDLE_CLICK] = false;
            return 0;
        }
        case WM_CHAR:
        {
            lastchar = wparam;
            return 0;
        }
        case WM_MOUSEWHEEL:
        {
            mouseX = LOWORD(lparam);
            mouseY = *winSizeY - HIWORD(lparam) - 1;
            if( HIWORD(wparam) == 120 ){
                setRoll(ROLL_UP);
            }else{
                setRoll(ROLL_DOWN);
            }
            return 0;
        }
    }
    return 1;
}
